package yja.gzw.app.chengjiafang

import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import cn.jzvd.Jzvd
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.kongzue.dialogx.dialogs.CustomDialog
import com.kongzue.dialogx.interfaces.OnBindView
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuikit.tuichat.classicui.page.TUIC2CChatActivity
import yja.gzw.app.R
import yja.gzw.app.activity.ItJvjueActivity
import yja.gzw.app.activity.JvjueActivity
import yja.gzw.app.activity.WodefabuActivity
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityChengjiadaiquerengBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class ChengjiadaiquerengActivity :BaseActivity<Mainviewmodel, ActivityChengjiadaiquerengBinding>(R.layout.activity_chengjiadaiquereng){

    val SDK_PAY_FLAG: Int = 1
    var userid=""
    val SDK_AUTH_FLAG: Int = 2
    var qian=""
    var types=""
    var leixing="wechat"
    override fun initView() {
        val ids =intent.getStringExtra("ids")

        binding.sixing.setOnClickListener {
            Log.e("userid2","userid2是"+userid)
            val intent = Intent(this, TUIC2CChatActivity::class.java)
            intent.putExtra(TUIConstants.TUIChat.CHAT_ID,userid)
            intent.putExtra(TUIConstants.TUIChat.CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
            startActivity(intent)


        }

        viewmodel.onxiangqingwang(UserData.token, mapOf("order_id" to ids!!))

        binding.chexiaodingdan.setOnClickListener {
//            finish()
//            val inten= Intent(this, ItJvjueActivity::class.java)
//            inten.putExtra("oids",ids)
//            startActivity(inten)


            CustomDialog.show(object : OnBindView<CustomDialog>(R.layout.layout_custom_tishiquxiao) {
                override fun onBind(dialog: CustomDialog, v: View) {
                    val tongyi = v.findViewById<TextView>(R.id.tongyi)
                    val butongyi = v.findViewById<TextView>(R.id.butongyi)

                    tongyi.setOnClickListener {

                        viewmodel.onquxiaoqiangdanwang(UserData.token, mapOf("order_id" to ids))
                    }

                    butongyi.setOnClickListener {
                        dialog.dismiss()
                    }



                }
            })
                .setMaskColor(this.getColor(R.color.touming))





        }






    }

    override fun dataObservese() {

        viewmodel.onquxiaoqiangdan.observe(this, Observer {
            if(it.code==1){

                finish()
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            }


        })


        viewmodel.onxiangqing.observe(this, Observer {
            if(it.code==1){

                if(it.data.fine_release_type==1){
                    binding.fabumoshioqian.text="自己定价"
                }else if(it.data.fine_release_type==2){
                    binding.fabumoshioqian.text="第二方定价"
                }else{
                    binding.fabumoshioqian.text="第二方竞价"
                }
                binding.platformId.text=it.data.platform_id
                binding.weizhi.text=it.data.address
                binding.neirong.text=it.data.content
                binding.huobiaoqian.text=it.data.need_title
                binding.wengzisucaishow.text= it.data.label
                binding.biaotifu.text=it.data.title


                Log.e("承接走到了","走到了")
                binding. videoView.setUp(it.data.video_url.toString(), "播放视频", Jzvd.SCREEN_WINDOW_NORMAL)
                binding. videoView.thumbImageView.setImageURI(Uri.parse("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"));
                types=it.data.type
                //todo 承接方的用户信息
                userid=it.data.publisher.id.toString()

                Log.e("承接方用户id",it.data.publisher.toString())

                binding.biaoti.text=it.data.title

                if(it.data.type=="1"){
                    binding.zhuanquleixing.text="三元视频"

                }else if(it.data.type=="2"){
                    binding.zhuanquleixing.text="四元视频"
                }else{
                    binding.zhuanquleixing.text="精品视频"
                }
                binding.fabumoshioqian.text="已出价￥${it.data.pay_price}"

                binding.wengzisucaishow.text=it.data.text_material
                val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
                Glide.with(this).load("http://117ai.32.hzgqapp.com/"+it.data.publisher.avatar)
                    .apply(requestOptions)
                    .into(binding.touxiang)
                binding.name.text=it.data.publisher.username
                binding.dingdanbiaohao.text=it.data.order_no
                val timestamp = 1739169752L
                // 将秒级时间戳转换为 Instant 对象
                //todo 爆红不必理会
                val instant = Instant.ofEpochSecond(timestamp)

                val zoneId = ZoneId.systemDefault()
                // 将 Instant 对象转换为 LocalDateTime 对象
                val localDateTime = instant.atZone(zoneId).toLocalDateTime()
                // 定义日期时间格式化模式
                val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                // 格式化日期时间
                val formattedDateTime = localDateTime.format(formatter)
                binding.dindanfabushijian.text=it.data.create_time_text
                binding.dindanchengjiaoshijian.text=it.data.confirm_time_text
                binding.dindanwanchengshijian.text=it.data.delivery_time_text
                binding.dindanjiage.text="￥${it.data.pay_price}"
                qian=it.data.price_list[0].price
                binding.dingjiaqianshu.text="第二方定价：￥${it.data.pay_price}"


            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })
    }


}